//
//  CSFilterMenuSubCategorysContentView.m
//  XiRan-iOS
//
//  Created by CSS on 6/18/23.
//  Copyright © 2023 Wood. All rights reserved.
//

#import "CSFilterMenuSubCategorysContentView.h"

#import "MWFilterModel.h"
#import "MWFilterMenuCell.h"

@interface CSFilterMenuSubCategorysContentView ()
<
 UICollectionViewDelegateFlowLayout
,UICollectionViewDataSource
>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowlayout;

@end

@implementation CSFilterMenuSubCategorysContentView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:245 / 255.0 alpha:1];
        
        self.flowlayout = [UICollectionViewFlowLayout new];
        self.flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowlayout];
        self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
        self.collectionView.backgroundColor = [UIColor clearColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self addSubview:self.collectionView];
        
        [_collectionView registerClass:[MWFilterMenuCell class] forCellWithReuseIdentifier:@"MWFilterMenuCell"];
        
        self.flowlayout.minimumLineSpacing = 12;
        self.flowlayout.minimumInteritemSpacing = 10;
        self.flowlayout.sectionInset = UIEdgeInsetsMake(5, 20, 5, 20);
        
        [NSLayoutConstraint activateConstraints:@[
            [self.collectionView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
            [self.collectionView.topAnchor constraintEqualToAnchor:self.topAnchor],
            [self.collectionView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
            [self.collectionView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
        ]];
    }
    return self;
}

- (void)setSubattributes:(NSArray *)subattributes
{
    _subattributes = subattributes;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.subattributes count];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((collectionView.frame.size.width - 40 - 12 - 1) / 2, 40);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"MWFilterMenuCell";
    MWFilterMenuCell *filterCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    filterCell.contentView.backgroundColor = [UIColor clearColor];
    [filterCell setItemButtonBackgroundColor:[UIColor whiteColor]];
   
    MWSubattribute *attribute = self.subattributes[indexPath.row];
    
    MWWeakSelf
    filterCell.block = ^{
        if ( attribute.isSelected) {
             attribute.isSelected = NO;
        } else {
             attribute.isSelected = YES;
        }
        [weakSelf.collectionView reloadData];
    };
    
#if DEBUG
    if(attribute.attr_value.length == 0) {
        filterCell.title = @"缺少翻译";
    } else {
        filterCell.title = attribute.attr_value;
    }
#else
    filterCell.title = attribute.attr_value;
#endif
    filterCell.isSelected = attribute.isSelected;
    
    return filterCell;
}

@end
